package com.xiaoyu.lanling.view

import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.xiaoyu.base.app.App
import com.xiaoyu.lanling.feature.user.options.MoreActionItemBase
import java.util.*

class MoreActionController {

    fun showMoreActionList(actionList: List<MoreActionItemBase>) {
        val context = App.getInstance().topActivity ?: return

        val list: List<MoreActionItemBase> = ArrayList(actionList)
        val len = list.size
        val items = arrayOfNulls<CharSequence>(len)
        for (i in 0 until len) {
            items[i] = list[i].title
        }
        val builder = AlertDialog.Builder(context)
        builder.setItems(items) { _: DialogInterface?, which: Int ->
            list[which].onClick()
        }
        builder.show()
    }

    companion object {

        fun show(list: List<MoreActionItemBase>): MoreActionController {
            val controller = MoreActionController()
            controller.showMoreActionList(list)
            return controller
        }
    }
}